package tech.aistar.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.*;
import tech.aistar.service.*;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import tech.aistar.Util.*;

@Controller
@RequestMapping("/Detail")
public class DetailInfoAction {
    @Autowired
    private IHouseService houseService;
    @Autowired
    private IAgentService agentService;
    @Autowired
    private ICostService costService;
    @Autowired
    private IAppointmentTimeService appointmentTimeService;
    @Autowired
    private ICotenancyRequireService cotenancyRequireService;
    @Autowired
    private IEnvironmentFacilityService environmentFacilityService;
    @Autowired
    private IConcernService concernService;
    @Autowired
    private IImageService iImageService;
    @Autowired
    private ISeeRoomTrendService seeRoomTrendService;

    @RequestMapping("/hezuxiangxi/{house_id}")
    public String detail3(@PathVariable("house_id") String house_id, HttpServletRequest request) {
        request.getSession().setAttribute("aaa",1);
        Integer tenant_id =null;
        if(request.getSession().getAttribute("tenant")!=null) {
            Tenant tenant = (Tenant) request.getSession().getAttribute("tenant");
            tenant_id = tenant.getId();
        }
        if(tenant_id !=null) {
            request.setAttribute("tenant_id", tenant_id);
        }else{
            request.setAttribute("tenant_id",0);
        }

        House house = houseService.selectById(Integer.valueOf(house_id));
        request.setAttribute("house", house);

        List<String> picture = iImageService.selectByhouseId(Integer.valueOf(house_id));
        request.setAttribute("picture",picture);

        int size = picture.size();
        request.setAttribute("size",size-1);

        Image picture_one = iImageService.selectOneByHouseId(Integer.valueOf(house_id));
        request.setAttribute("picture_one",picture_one.getPicture());

        Cost cost = costService.findByhouseId(Integer.valueOf(house_id));
        request.setAttribute("cost", cost);

        CotenancyRequire cotenancyRequire = cotenancyRequireService.findByhouseId(Integer.valueOf(house_id));
        request.setAttribute("cotenancyRequire", cotenancyRequire);

        EnvironmentFaclity environmentFaclity = environmentFacilityService.findByhouseId(Integer.valueOf(house_id));
        request.setAttribute("environmentFacility", environmentFaclity);

        Integer agent_id=houseService.selectagentByhouseId(Integer.valueOf(house_id));

        Agent agent = agentService.selectById(Integer.valueOf(agent_id));
        request.setAttribute("agent", agent);

        Concern concern = concernService.selectById(tenant_id, Integer.valueOf(house_id));
        request.setAttribute("concern", concern);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        seeRoomTrendService.save2Rbm(house_id);

        //获取本周七天的日期
        List<Date> lDate = dateUtil.getDays(7);
        int i = 1;
        for (Date date : lDate)
        {
        SeeRoomTrend seeRoomTrend=seeRoomTrendService.selectByHouseId(Integer.valueOf(house_id),date);

           if(seeRoomTrend==null){
               request.setAttribute("t"+i,0);
            request.setAttribute("d"+i,sdf.format(date));
               i++;

           }
           else{
               request.setAttribute("d"+i,sdf.format(date));
            request.setAttribute("t"+i,seeRoomTrend.getSeeCount());
            i++;
            System.out.println(seeRoomTrend.getSeeCount());
           }
        }


        return "/rent_info/hezufangyuanxinxi";
    }

    @RequestMapping("/zhengzuxiangxi/{house_id}")
    public String detail4(@PathVariable("house_id") String house_id, HttpServletRequest request) {
        request.getSession().setAttribute("aaa",1);
        Integer tenant_id =null;
        if(request.getSession().getAttribute("tenant")!=null) {
            Tenant tenant = (Tenant) request.getSession().getAttribute("tenant");
            tenant_id = tenant.getId();
        }
        if(tenant_id !=null) {
            request.setAttribute("tenant_id", tenant_id);
        }else{
            request.setAttribute("tenant_id",0);
        }

        House house = houseService.selectById(Integer.valueOf(house_id));
        request.setAttribute("house", house);

        List<String> picture = iImageService.selectByhouseId(Integer.valueOf(house_id));
        request.setAttribute("picture",picture);

        int size = picture.size();
        request.setAttribute("size",size-1);

        Image picture_one = iImageService.selectOneByHouseId(Integer.valueOf(house_id));
        request.setAttribute("picture_one",picture_one.getPicture());

        Cost cost = costService.findByhouseId(Integer.valueOf(house_id));
        request.setAttribute("cost", cost);

        CotenancyRequire cotenancyRequire = cotenancyRequireService.findByhouseId(Integer.valueOf(house_id));
        request.setAttribute("cotenancyRequire", cotenancyRequire);

        EnvironmentFaclity environmentFaclity = environmentFacilityService.findByhouseId(Integer.valueOf(house_id));
        request.setAttribute("environmentFacility", environmentFaclity);

        Integer agent_id=houseService.selectagentByhouseId(Integer.valueOf(house_id));

        Agent agent = agentService.selectById(Integer.valueOf(agent_id));
        request.setAttribute("agent", agent);

        Concern concern = concernService.selectById(tenant_id, Integer.valueOf(house_id));
        request.setAttribute("concern", concern);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        seeRoomTrendService.save2Rbm(house_id);

        //获取本周七天的日期
        List<Date> lDate = dateUtil.getDays(7);
        int i = 1;
        for (Date date : lDate)
        {
            SeeRoomTrend seeRoomTrend=seeRoomTrendService.selectByHouseId(Integer.valueOf(house_id),date);

            if(seeRoomTrend==null){
                request.setAttribute("t"+i,0);
                request.setAttribute("d"+i,sdf.format(date));
                i++;

            }
            else{
                request.setAttribute("d"+i,sdf.format(date));
                request.setAttribute("t"+i,seeRoomTrend.getSeeCount());
                i++;
                System.out.println(seeRoomTrend.getSeeCount());
            }
        }



        return "/rent_info/zhengzufangyuanxinxi";
    }



}



